package com.example.myliwu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends AppCompatActivity {
    private EditText usernamek;
    private EditText upwdk;
    private CheckBox jzmmk;
    private Button dlan;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (getSupportActionBar()!=null){
            getSupportActionBar().hide();
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
            );
        }
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        usernamek = (EditText) findViewById(R.id.username);
        upwdk = (EditText) findViewById(R.id.upwd);
        dlan = (Button) findViewById(R.id.btn_dl);
        jzmmk = (CheckBox) findViewById(R.id.jzmm);
        boolean isRemember = pref.getBoolean("remem_pswd", false);
        if (isRemember) {
            String yhm = pref.getString("account", "");
            String pwd = pref.getString("pwd", "");
            usernamek.setText(yhm);
            upwdk.setText(pwd);
            jzmmk.setChecked(true);
        }
    }

    public void onClick(View view) {
        String username = usernamek.getText().toString().trim();
        String upwd = upwdk.getText().toString().trim();
        if (username.equals("admin") && upwd.equals("123")) {
            editor = pref.edit();
            if (jzmmk.isChecked()) {
                editor.putBoolean("remem_pswd", true);
                editor.putString("account", username);
                editor.putString("passw", upwd);
            } else {
                editor.clear();
            }
            editor.apply();
            Toast.makeText(getApplicationContext(), "登陆成功！", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Login.this, MainActivity2.class);
            startActivity(intent);
        } else {
            Toast.makeText(getApplicationContext(), "用户名密码错误！", Toast.LENGTH_LONG).show();
        }
    }
}